﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.Entity;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.UI;

namespace LEARNREN.COM.WebSite
{
    public partial class ArticleList : BasePage
    {
        protected IEnumerable<Catalogue> productCatalogueList = null;
        protected IEnumerable<Catalogue> childCatalogueList = null;
        protected Catalogue curentCatalogue = null;
        protected List<Article> articleList = null;
        protected int pageSize = 17;
        protected int curPage = 1;
        protected int totalPage = 1;
        protected int totalArticle = 0;

        private ArticleLogic articleLogic = null;
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void Initialize()
        {
            productCatalogueList = catalogueLogic.GetConfigedProductCatalogueCollection();

            string sid = Request.QueryString["id"];
            long id = 0;
            if (long.TryParse(sid, out id))
            {
                childCatalogueList = catalogueLogic.GetChildren(id);
                curentCatalogue = catalogueLogic.GetByIdFromCache(id);
                if (curentCatalogue == null)
                {
                    Response.Redirect("~/");
                }

                string spage = Request.QueryString["page"];
                if (string.IsNullOrEmpty(spage))
                {
                    spage = "1";
                }

                int page = 1;
                if (int.TryParse(spage, out page))
                {
                    articleList = articleLogic.GetListDESC(p => p.CatalogueId == curentCatalogue.Id, p => p.Id);
                    totalArticle = articleList.Count;

                    totalPage = (int)Math.Ceiling((double)totalArticle / pageSize);

                    if (page <= 0)
                    {
                        curPage = 1;
                    }
                    else if (page > totalPage)
                    {
                        curPage = totalPage;
                    }
                    else
                    {
                        curPage = page;
                    }

                    articleList = articleList.Skip(pageSize * (curPage - 1)).Take(pageSize).ToList();
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
            else
            {
                Response.Redirect("~/");
            }
        }

        protected override void InitLogic()
        {
            articleLogic = new ArticleLogic();
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (articleLogic != null)
            {
                articleLogic.Dispose();
                articleLogic = null;
            }

            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }
    }
}